package com.firestone.service.center.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.firestone.bean.dto.resource.ResourceListDTO;
import com.firestone.bean.entity.mysql.primary.ImCenterResource;
import com.firestone.core.constants.ConstantKey;
import com.firestone.dao.primary.center.ImCenterResourceDao;
import com.firestone.dao.primary.center.ImCenterTerminalMenuDao;
import com.firestone.service.center.IImCenterResourceService;

/**
 * <p>
 * 系统资源库表 服务实现类
 * </p>
 *
 * @author CAIYJ
 * @since 2018-03-27
 */
@Service
public class ImCenterResourceServiceImpl
        extends ServiceImpl<ImCenterResourceDao, ImCenterResource>
        implements IImCenterResourceService {

    @Autowired
    private ImCenterTerminalMenuDao imCenterTerminalMenuDao;

    @SuppressWarnings({ "unchecked" })
    @Override
    public List<Object> listPermissionUrl(Long id) throws Exception {
        List<String> urlIds = imCenterTerminalMenuDao.getUrlsByManagerId(id);
        if (null != urlIds && 0 < urlIds.size()) {
            List<String> ids = new ArrayList<>();
            for (String urlId : urlIds) {
                ids.addAll(
                        Arrays.asList(urlId.split(ConstantKey.COMMA_SYMBOL_E)));
            }
            Condition condition = new Condition();
            condition.setSqlSelect("url");
            condition.in("id", ids);
            return this.baseMapper.selectObjs(condition);
        }
        return null;
    }

    @Override
    public Page<ResourceListDTO> listResource(Page<ResourceListDTO> page,
            String name) throws Exception {
        EntityWrapper<ResourceListDTO> ew = new EntityWrapper<ResourceListDTO>();
        ew.setEntity(new ResourceListDTO());
        ew.where(StringUtils.isNotBlank(name),
                "cr.`name` LIKE concat('%',{0},'%')", name);
        String[] order = { "cr.update_time" };
        ew.orderDesc(Arrays.asList(order));
        List<ResourceListDTO> list = this.baseMapper.selectResourceList(page,
                ew);
        return page.setRecords(list);
    }

}
